﻿/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Linq;
using Bio.Data.Interfaces;

namespace Bio.Data
{
    /// <summary>
    /// This class implements standard IBioValidator types.
    /// </summary>
    public class BioValidator : IBioValidator
    {
        /// <summary>
        /// Validator for rRNA
        /// Reference: Cornish-Bowden, A. 1985. Nomenclature for incompletely specified
        /// bases in nucleic acid sequences: recommendations 1984. 
        /// Nucleic Acid Research 13:3021-3030.
        /// 
        /// Additional reference:
        /// http://www.dnabaser.com/articles/IUPAC%20ambiguity%20codes.html
        /// </summary>
        public static BioValidator rRNAValidator = new BioValidator("ABCDGHKMNRSTUVWY", BioSymbolType.Nucleotide, new[] { BioSymbolType.Missing, BioSymbolType.None });

        private readonly BioSymbolType[] _validTypes;
        private readonly BioSymbolType _primaryType;
        private readonly string _validSymbols;
        
        /// <summary>
        /// Constructor for validator
        /// </summary>
        /// <param name="validSymbols">List of valid symbols</param>
        /// <param name="primaryType">Primary Type associated with symbols</param>
        /// <param name="otherTypes">Other possible types allowed w/o symbol check</param>
        protected BioValidator(string validSymbols, BioSymbolType primaryType, BioSymbolType[] otherTypes)
        {
            if (string.IsNullOrEmpty(validSymbols))
                throw new ArgumentOutOfRangeException("validSymbols must contain at least one character symbol.");
            
            _validSymbols = validSymbols;
            _validTypes = otherTypes;
            _primaryType = primaryType;
        }

        /// <summary>
        /// Returns whether the given BioSymbol is valid
        /// </summary>
        /// <param name="bioSymbol"></param>
        /// <returns></returns>
        public bool IsValid(IBioSymbol bioSymbol)
        {
            return (bioSymbol.Type == _primaryType && IsValid(bioSymbol.Value)) 
                || (_validTypes != null && _validTypes.Contains(bioSymbol.Type));
        }

        /// <summary>
        /// Returns whether the given character is valid
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public bool IsValid(char ch)
        {
            return _validSymbols.IndexOf(Char.ToUpper(ch)) >= 0;
        }
    }
}
